<?php
namespace Home\Controller;
use Think\Controller;

// +----------------------------------------------------------------------
// | Fanwe 方维p2p借贷系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class BaseController extends Controller{
    //后台基础类构造
    protected $lang_pack;
    public function __construct()
    {
        parent::__construct();
        check_install();
        $GLOBALS['request'] = $_REQUEST;
        $GLOBALS['db']= M();
        if(file_exists(APP_ROOT_PATH.'/system/template/template.php')){
            require_once APP_ROOT_PATH.'/system/template/template.php';
            $GLOBALS['tmpl'] = new \AppTemplate ;
        }
        //require_once APP_ROOT_PATH.'/system/common.php';
        //重新处理后台的语言加载机制，后台语言环境配置于后台config.php文件
        $langSet = conf('DEFAULT_LANG');
        // 定义当前语言
        define('LANG_SET',strtolower($langSet));
        // 读取项目公共语言包
        if (is_file(LANG_PATH.$langSet.'/common.php'))
        {
            L(include LANG_PATH.$langSet.'/common.php');
            $this->lang_pack = require LANG_PATH.$langSet.'/common.php';

            if(!file_exists(APP_ROOT_PATH."public/runtime/admin/lang.js"))
            {
                $str = "var LANG = {";
                foreach($this->lang_pack as $k=>$lang)
                {
                    $str .= "\"".$k."\":\"".$lang."\",";
                }
                $str = substr($str,0,-1);
                $str .="};";
                file_put_contents(APP_ROOT_PATH."public/runtime/admin/lang.js",$str);
            }
        }
        $GLOBALS['user_info'] = \es_session::get('user_info');
        //var_dump(   $GLOBALS['user_info'] ) ;exit;
        //\es_session::close();
    }

    public function check_is_login(){
        $is_login =  0 ;
        if($user_info = \es_session::get("user_info")){
            $is_login =  1 ;
        }
        return $is_login ;
    }

    protected function error($message,$ajax = 0)
    {

        if(!$this->get("jumpUrl"))
        {
            if($_SERVER["HTTP_REFERER"]) $default_jump = $_SERVER["HTTP_REFERER"]; else $default_jump = u("Index/main");
            $this->assign("jumpUrl",$default_jump);
        }
        parent::error($message,$ajax);
    }
    protected function success($message,$ajax = 0)
    {

        if(!$this->get("jumpUrl"))
        {
            if($_SERVER["HTTP_REFERER"]) $default_jump = $_SERVER["HTTP_REFERER"]; else $default_jump = u("Index/main");
            $this->assign("jumpUrl",$default_jump);
        }
        parent::success($message,$ajax);
    }
    function getControllerName(){
        return CONTROLLER_NAME;
    }
}
?>